//
//  Second.swift
//  mng
//
//  Created by WTFKL on 2018/4/3.
//  Copyright © 2018年 WTF. All rights reserved.
//


import GameplayKit
import SpriteKit

class Second: GameScene {
    private var partition : SKSpriteNode?;
    private var partition2 : SKSpriteNode?;
    private var partition3 : SKSpriteNode?;
    private var partition4 : SKSpriteNode?;
    private var groundPart2 : SKSpriteNode?;
    private var groundPart3 : SKSpriteNode?;
    private var keySprite : SKSpriteNode?;
    private var realkeySprite : SKSpriteNode?;
    private var ship : SKSpriteNode?;
    private var ball : SKSpriteNode?;
    
    override func didMove(to view: SKView) {
        super.didMove(to: view);
        initWorld();
        
    }
    
    func initWorld(){
        tips?.fontColor = UIColor.init(red: 64/255.0, green: 169/255.0, blue: 216/255.0, alpha: 1);
        mesprite?.physicsBody?.friction = 0;
        ground?.physicsBody?.friction = 0;
        mesprite?.physicsBody?.linearDamping = 0.5;
        doorWall?.texture = SKTexture(image: #imageLiteral(resourceName: "second-door"));
        
        backgroundPC?.texture = SKTexture(image: #imageLiteral(resourceName: "second-bg"));
        
        tips?.text = "提示：或许你该注意脚底？";
        let groundWidth = phoneType! ? 250 : 280;
        ground?.size = CGSize(width: groundWidth, height: 100);
        ground?.texture = SKTexture(image: #imageLiteral(resourceName: "ground-left"));
        
        ground?.position = CGPoint(x: (-screenWidth+(ground?.size.width)!)/2, y: (ground?.position.y)!);
        ground?.physicsBody = SKPhysicsBody(rectangleOf: (ground?.size)!);
        ground?.physicsBody?.affectedByGravity = false;
        ground?.physicsBody?.isDynamic = false;
        ground?.physicsBody?.restitution = 0;
        ground?.physicsBody?.categoryBitMask = BitMaskType.ground;
        ground?.physicsBody?.contactTestBitMask = BitMaskType.charactor;//能被角色碰撞
        ground?.zPosition = -1;
        //隔板
        
//        partition = SKSpriteNode(color: .black, size: CGSize.init(width: 100, height: 20));
        partition = SKSpriteNode(texture: SKTexture(image: UIImage.init(named: "s-bar1")!));
        let partitionW = (partition?.size.width)!/2;
        partition?.position = CGPoint(x: -screenWidth/2 + partitionW, y: 25);
        partition?.physicsBody = SKPhysicsBody(rectangleOf: (partition?.frame.size)!);
        partition?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition?.physicsBody?.affectedByGravity = true;
        partition?.physicsBody?.isDynamic = false;
//        partition?.isHidden = true;
        self.addChild(partition!);
        let partition2Y = phoneType! ? -30 : -20;
//        partition2 = SKSpriteNode(color: .black, size: CGSize.init(width: 50, height: 20));
        partition2 = SKSpriteNode(texture: SKTexture(image: UIImage.init(named: "s-bar4")!));
        partition2?.position = CGPoint(x: -120, y: partition2Y);
        partition2?.physicsBody = SKPhysicsBody(rectangleOf: (partition2?.frame.size)!);
        partition2?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition2?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition2?.physicsBody?.affectedByGravity = false;
        partition2?.physicsBody?.isDynamic = false;
        self.addChild(partition2!);
        
        partition3 = SKSpriteNode(texture: SKTexture(image: UIImage.init(named: "s-bar3")!));
        partition3?.position = CGPoint(x: 100, y: 120);
        partition3?.physicsBody = SKPhysicsBody(rectangleOf: (partition3?.frame.size)!);
        partition3?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition3?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition3?.physicsBody?.affectedByGravity = true;
        partition3?.physicsBody?.isDynamic = false;
//        partition3?.isHidden = true;
        self.addChild(partition3!);
        //小的
        partition4 = SKSpriteNode(texture: SKTexture(image: UIImage.init(named: "s-bar2")!));
        partition4?.position = CGPoint(x: -50, y: 80);
        partition4?.physicsBody = SKPhysicsBody(rectangleOf: (partition4?.frame.size)!);
        partition4?.physicsBody?.categoryBitMask = BitMaskType.board;
        partition4?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        partition4?.physicsBody?.affectedByGravity = true;
        partition4?.physicsBody?.isDynamic = false;
//        partition4?.isHidden = true;
        self.addChild(partition4!);
        
        let groundPart3Width = phoneType! ? 280 : 320 ;
        groundPart3 = SKSpriteNode(texture: SKTexture(image: #imageLiteral(resourceName: "ground-disappear")), size: CGSize.init(width: groundPart3Width, height: 100));
        groundPart3?.position = CGPoint(x: (ground?.position.x)!+(ground?.size.width)!/2+(groundPart3?.size.width)!/2, y: (ground?.position.y)!);
        groundPart3?.physicsBody = SKPhysicsBody(rectangleOf: (groundPart3?.frame.size)!);
        groundPart3?.physicsBody?.categoryBitMask = BitMaskType.SpecialGround;
        groundPart3?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        groundPart3?.physicsBody?.collisionBitMask = BitMaskType.damageThing;
        groundPart3?.physicsBody?.affectedByGravity = false;
        groundPart3?.physicsBody?.mass = 80;
        groundPart3?.physicsBody?.isDynamic = false;
        groundPart3?.zPosition = -1;
        groundPart3?.physicsBody?.restitution = 0;
        self.addChild(groundPart3!);
        
        groundPart2 = SKSpriteNode(texture: SKTexture(image: #imageLiteral(resourceName: "ground-right")), size: CGSize.init(width: (screenWidth - (groundPart3?.size.width)! - (ground?.size.width)!), height: 100));
        groundPart2?.position = CGPoint(x:(screenWidth-(groundPart2?.size.width)!)/2,y:(ground?.position.y)!);
        groundPart2?.physicsBody = SKPhysicsBody(rectangleOf: (groundPart2?.frame.size)!);
        groundPart2?.physicsBody?.categoryBitMask = BitMaskType.ground;
        groundPart2?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        groundPart2?.physicsBody?.affectedByGravity = true;
        groundPart2?.physicsBody?.isDynamic = false;
        groundPart2?.zPosition = 1;
        groundPart2?.physicsBody?.restitution = 0;
        self.addChild(groundPart2!);
        
//
//        ship = SKSpriteNode(color: .clear, size: CGSize.init(width: (ground?.size.height)!, height: (ground?.size.height)!));
        ship = SKSpriteNode(texture: SKTexture(image: #imageLiteral(resourceName: "ground-move")));
        ship?.size = CGSize.init(width: (ground?.size.height)!, height: (ground?.size.height)!);
        ship?.position = CGPoint(x: (groundPart2?.position.x)! - (groundPart2?.size.width)!/2 - (ship?.size.width)!/2, y: (ground?.position.y)!);
        ship?.physicsBody = SKPhysicsBody(rectangleOf: (ship?.frame.size)!);
        ship?.physicsBody?.categoryBitMask = BitMaskType.SpecialGround;
        ship?.physicsBody?.contactTestBitMask = BitMaskType.charactor;
        ship?.physicsBody?.collisionBitMask = BitMaskType.charactor;
        ship?.physicsBody?.affectedByGravity = false;
        ship?.physicsBody?.isDynamic = false;
        ship?.zPosition = -2;
        
        
        
        //key
        keySprite = MeKey(mName: "key", posistion: CGPoint(x: (groundPart2?.position.x)!-50, y: (groundPart2?.position.y)!+80));
        self.addChild(keySprite!);
        
    }
    
    override func didBegin(_ contact: SKPhysicsContact) {
        super.didBegin(contact);
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask == BitMaskType.charactor|BitMaskType.SpecialGround ) {
            isOnGround = false;
            if(contact.bodyB.node == ship){//接触到移动块
                isOnGround = true;
                ship?.color = .black;
                let moveAction = SKAction.move(to: CGPoint(x: (ground?.position.x)! + (ground?.size.width)!/2 + (ship?.size.width)!/2, y: (ship?.position.y)!), duration: 3);
                ship?.run(moveAction);
            }else {
                groundPart3?.removeFromParent();
                isOnGround = true;
            }
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.key) {//人物与钥匙
            
//            let mykeyNode = contact.bodyB.node as? MeKey;
            
            if(contact.bodyB.node == keySprite){
                keySprite?.removeFromParent();
                self.addChild(ship!);
                realkeySprite = MeKey(mName: "realkey", posistion: CGPoint(x: (partition?.position.x)!, y: (partition?.position.y)!+(keySprite?.size.height)!/2+10));
                self.addChild(realkeySprite!);
            }else if (contact.bodyB.node == realkeySprite){
                doorWall?.texture = SKTexture(image: #imageLiteral(resourceName: "second-open-door"));
                mesprite?.isGetKey = true;
                realkeySprite?.removeFromParent();
            }
        }
        
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == (BitMaskType.charactor | BitMaskType.doorWall) {//人物与过关触发器
            if(mesprite?.isGetKey == true){
                self.view?.presentScene(SKScene(fileNamed: "Third")!,transition: SKTransition.crossFade(withDuration: 0.5));
            }
        }
    }
    
    func didEnd(_ contact: SKPhysicsContact) {
        if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask == BitMaskType.line|BitMaskType.SpecialGround ) {
            isOnGround = true;
        }
    }
    
    func charactorIsAlive(){
        if (mesprite?.position.y)! <= -157{
            mesprite?.isAlive = false;
        }
        if mesprite?.isAlive == false {
            self.view?.presentScene(SKScene(fileNamed: "Second")!,transition: SKTransition.crossFade(withDuration: 0.5));
        }
    }
    
    let minSpead : CGFloat = -100.0;
    let maxSpead : CGFloat = 100.0;
    
    override func update(_ currentTime: TimeInterval) {
//        super.update(currentTime);
        charactorIsAlive();
        
        switch myState {
        case .left:
            mesprite?.physicsBody?.applyImpulse(CGVector(dx: -3, dy: 0));
            if( (mesprite?.physicsBody?.velocity.dx)! < minSpead ){
                mesprite?.physicsBody?.velocity.dx = minSpead;
            }
        
        case .right:
            mesprite?.physicsBody?.applyImpulse(CGVector(dx: 3, dy: 0));
            if( (mesprite?.physicsBody?.velocity.dx)! > maxSpead ){
                mesprite?.physicsBody?.velocity.dx = maxSpead;
            }
        default:
            break;
        
        }
    }
}
